package com.ciban.smarthome.module.engine;

import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.db.dao.EquipmentDao;
import com.ciban.smarthome.module.db.dao.LampDao;
import com.ciban.smarthome.module.engine.base.BaseEngine;
import com.ciban.smarthome.module.entity.Equip_Lamp;
import com.ciban.smarthome.module.entity.Host;
import com.ciban.smarthome.module.net.communication.Communicate;
import com.ciban.smarthome.module.net.communication.ProtocolHelper;
import com.ciban.smarthome.module.net.protocol.Protocol;
import com.ciban.smarthome.module.utils.ModuleUtils;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/6.
 * 电灯的业务逻辑类
 */
public class LampEngine extends BaseEngine {
    private LampDao dao;
    private Host host;

    private ArrayList<Equip_Lamp> lightList;
    private int type;//电器类型
    private String area_id;

    public LampEngine(Context context){
        super(context);
    }

    public LampEngine(Context context, int type, String area_id) {
        super(context);
        this.type = type;
        this.area_id = area_id;

        host = getHostInfo(area_id);
    }


    public ArrayList<Equip_Lamp> getLightList() {
        if (lightList == null) {
            lightList = getLampListFromDb(area_id, type);
        }
        return lightList;
    }

    /**
     * 刷新电灯列表
     */
    public void refreshLightList() {
        lightList.clear();
        lightList.addAll(getLampListFromDb(area_id, type));
    }

    /**
     * 从数据库中获取电灯列表
     *
     * @param area_id
     * @param type
     * @return
     */
    private ArrayList<Equip_Lamp> getLampListFromDb(String area_id, int type) {
        checkDao();
        return dao.getLampList(area_id,type);
    }


    /**
     * 添加新的电灯
     *
     * @param info
     * @return
     */
    public boolean addNewLamp(Equip_Lamp info) {
        checkDao();
        String id = addNewEquipMent(info);
        LogUtil.info("添加新的电灯的id："+id);
        if (!StringUtils.isEmpty(id)) {
            LogUtil.info("准备添加电灯配置");
            info.setId(id);
            return dao.addNewLamp(info) > 0;
        }
        return false;
    }

    /**
     * 更新电灯信息
     *
     * @param info
     * @return
     */
    public boolean updateLamp(Equip_Lamp info) {
        checkDao();
        upEquipment(info);
        return dao.upLamp(info) > 0;
    }

    /**
     * 删除电灯
     * @param info
     * @return
     */
    public boolean delLamp(Equip_Lamp info) {
        checkDao();
        if( dao.delLamp(info.getId()) > 0){
            lightList.remove(info);
            return true;
        }
        return false;
    }

    /**
     * 发送命令
     *
     * @param info
     * @param cmd_type
     */
    public void sendCmd(Equip_Lamp info, int cmd_type, Communicate.ConnListener listener) {
        String cmd = ProtocolHelper.getNormalCmd(host,
                info.getSerial_no(), cmd_type);
        sendData(cmd, host, listener);
    }

    public void sendCmd(String cmd, Communicate.ConnListener listener) {
        sendData(cmd, host, listener);
    }

    private String status = "";

    /**
     * 解析电灯状态
     */
    public void parseLampStatus(String statsList) {
        status = statsList.substring(12, 38);
        for (Equip_Lamp info : lightList) {
            if (info.getStatus() == -1)
                info.setStatus(ModuleUtils.getEquipmentStatus(
                        info.getSerial_no(), status));
        }
    }


    public void destroy() {
        dao = null;
    }

    private void checkDao() {
        if (dao == null)
            dao = new LampDao(context);
    }

}
